package ysq.xyg.ticket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.ticket.dao.TicketRecordDao;
import ysq.xyg.ticket.model.po.TicketRecord;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.ticket.model.vo.TicketRecordVo;
import ysq.xyg.ticket.service.TicketRecordService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 票卷模块-账单流水service实现
 *
 * @author shengQiang yu
 * @date 2019-10-30 10:23:02
 */
@Service("ticketRecordService")
public class TicketRecordServiceImpl extends BaseServiceImpl<TicketRecord> implements TicketRecordService {

    @Resource
    private TicketRecordDao ticketRecordDao;

    private TicketRecord ticketRecord;

    @Autowired
    public void setBaseDao(TicketRecordDao ticketRecordDao) {
        this.baseDao = ticketRecordDao;
    }


    /**
     * 分页查询
     *
     * @param ticketRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-30 10:23:02
     */
    public ReturnEntity page(TicketRecordVo ticketRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        if(StrUtil.isNotEmpty(ticketRecordVo.getNickName())){
            ticketRecordVo.setNickName(BaseUtil.encodeBase64String(ticketRecordVo.getNickName()));
        }
        List<TicketRecord> list = ticketRecordDao.listLeft(ticketRecordVo);
        if(list!=null&&list.size()>0){
            for(TicketRecord ticketRecord:list){
                if(StrUtil.isNotEmpty(ticketRecord.getNickName())){
                    ticketRecord.setNickName(BaseUtil.decodeBase64(ticketRecord.getNickName()));
                }
            }
        }
        PageInfo<TicketRecord> page=new PageInfo<TicketRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param ticketRecordVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-30 10:23:02
     */
    public ReturnEntity addOrUpdate(TicketRecordVo ticketRecordVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(ticketRecordVo.getId())){
            ticketRecord=new TicketRecord(currentUser);
            isUpate=false;
        }else{
            ticketRecord=selectByPrimaryKey(ticketRecordVo.getId());
            if(ObjectUtil.isNull(ticketRecord)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ticketRecord.setUpdateTime(new Date());
            ticketRecord.setUpdateUser(currentUser.getId());
        }
        ticketRecord.setMemberId(ticketRecordVo.getMemberId());
        ticketRecord.setRecordType(ticketRecordVo.getRecordType());
        ticketRecord.setExtendId(ticketRecordVo.getExtendId());
        ticketRecord.setMoney(ticketRecordVo.getMoney());
        ticketRecord.setRemarks(ticketRecordVo.getRemarks());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(ticketRecord);
        }else{
            num=insert(ticketRecord);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-30 10:23:02
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        TicketRecord ticketRecord=selectByPrimaryKey(id);
        if(ticketRecord!=null){
            ticketRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ticketRecord.setUpdateTime(new Date());
            ticketRecord.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(ticketRecord);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ticketRecordVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-30 10:23:02
     */
    public List<TicketRecord> acceptList(TicketRecordVo ticketRecordVo){
        Example example = new Example(TicketRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(ticketRecordVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",ticketRecordVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return ticketRecordDao.selectByExample(example);
    }
}
